import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


public class GUI extends JPanel{

	JTextArea text;
	JTextArea label;
	GridBagConstraints c;
	JScrollPane scroll;
	public GUI()
	{
		super(new BorderLayout());
		text = new JTextArea();
		text.setLineWrap(true);
		text.setBackground(Color.WHITE);
		label = new JTextArea();
		label.setBackground(Color.darkGray);
		label.setLineWrap(true);
		text.setVisible(true);
		KeyListener listener = new KeyPressed();
		text.addKeyListener(listener);
		label.setFocusable(false);
		scroll = new JScrollPane (label, 
				   JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		
		this.add(scroll,BorderLayout.CENTER);
		this.add(text,BorderLayout.SOUTH);
	}
	
	public void typeMessage()
	{
		text.setText("");
		text.requestFocus();
	}
	public void sendMessage()
	{
		String temp = text.getText();
		if(temp.startsWith("\n"))
		{
			temp = temp.substring(1);
		}
		text.setText("");
		display(temp);
	}
	public void display(String str)
	{
		label.append(str + "\n");
		label.setForeground(Color.BLACK);
		label.setAutoscrolls(true);
		int x;
		label.selectAll();
		x = label.getSelectionEnd();
		label.select(x,x);
	}
	private class KeyPressed implements KeyListener
	{
		@Override
		public void keyPressed(KeyEvent e) {
			int var = e.getKeyCode();
			if(var == KeyEvent.VK_ENTER)
			{
				sendMessage();
			}		
			
		}
		@Override
		public void keyReleased(KeyEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		@Override
		public void keyTyped(KeyEvent arg0) {
			// TODO Auto-generated method stub
			
		}
	}

}
